/** \file gr_aligned_allocator.h  Provides simd aligned memory. */

#pragma once

#include "gr_aligned_allocator.h"

namespace gr {
    
    const int g_alignment = 16;

    unsigned char* alignedAlloc(size_t size)
    {
        unsigned char* p = new unsigned char[size + g_alignment];
        size_t offset = g_alignment - (size_t(p) & (g_alignment - 1));

        unsigned char* result = p + offset;
        result[-1] = (unsigned char)offset;

        return result;
    }

    void alignedFree(void* p)
    {
        if (p)
        {
            unsigned char* mem = (unsigned char*)p;
            mem = mem - mem[-1];
            delete [] mem;
        }
    }
}
